package it.uai.domainsMigrator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.swing.DebugGraphics;

import org.apache.log4j.Logger;

public class MailController {

	private static final Logger logger = Logger.getLogger(MailController.class);

	private ArrayList<Recipient> recipients = new ArrayList<Recipient>();
	private String sender;
	private String subject;
	private String message;
	private MailSender mailSender;

	public MailController(String sender, String subject, String recipientsFile,
			String messageFile) {

		this.sender = sender;
		this.subject = subject;
		this.message = readMessageFile(messageFile);

		BufferedReader br = null;

		try {

			String sCurrentLine;

			br = new BufferedReader(new FileReader(recipientsFile));

			while ((sCurrentLine = br.readLine()) != null) {
				if (!sCurrentLine.startsWith("#") & !sCurrentLine.isEmpty()) {
					String[] valori = sCurrentLine.split(",");
					Recipient dest = new Recipient();
					dest.setName(valori[0]);
					dest.setEmail(valori[1]);
					dest.setFtpHost(valori[2]);
					dest.setUsername(valori[3]);
					dest.setPassword(valori[4]);
					dest.setBasePath(valori[5]);
					recipients.add(dest);
					logger.debug("add new recipient");
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

	}

	public void setConnectionParameters(String smtpHost, String smtpPort,
			String smtpUsername, String smtpPassword) throws AddressException,
			MessagingException {
		mailSender = new MailSender(smtpHost, smtpPort, smtpUsername,
				smtpPassword);
	}

	public void sendMail() throws AddressException, MessagingException {

		for (Recipient r : recipients) {

			mailSender.send(sender, r.getEmail(), subject.replaceAll("\\$dominio", r.getName()), getContent(r, message));

		}

	}

	private String getContent(Recipient rec, String messaggio) {

		String mess = messaggio;

		mess = mess.replaceAll("\\$dominio", rec.getName());
		mess = mess.replaceAll("\\$username", rec.getUsername());
		mess = mess.replaceAll("\\$password", rec.getPassword());
		mess = mess.replaceAll("\\$ftphost", rec.getFtpHost());
		mess = mess.replaceAll("\\$basepath", rec.getBasePath());

		return mess;

	}

	private String readMessageFile(String filename) {
		
		logger.debug("FileName messaggio: " + filename);

		String content = null;
		File file = new File(filename);
		try {
			FileReader reader = new FileReader(filename);
			char[] chars = new char[(int) file.length()];
			reader.read(chars);
			content = new String(chars);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;

	}
}
